package zucc.edu.repay.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import Bean.user;
import Util.BaseException;
import Util.HibernateUtil;

public class UserDao {
	public user searchUser(String userId) throws BaseException{
		// TODO �Զ����ɵķ������
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = s.beginTransaction();
		String hql = "from user where userId = '" + userId + "'";
		Query qry = s.createQuery(hql);
		Object user = qry.uniqueResult();
		tx.commit();
		return (user)user;
	}
	
	public List<user> serarchAlluser(){//�������û�
		Session s = HibernateUtil.getSessionFactory().getCurrentSession(); 
		Transaction tx = s.beginTransaction();
        String hql = "from user where userPermissions ='user'";  
        Query query = s.createQuery(hql);  
        List<user> roles = query.list();  
        tx.commit();
        return roles; 
}
	
	public void delUser(user user) {
		// TODO �Զ����ɵķ������
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = s.beginTransaction();
		s.delete(user);
		tx.commit();
		
	}
	
	public void addUser(user user) {
		// TODO �Զ����ɵķ������
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = s.beginTransaction();
		s.save(user);
		tx.commit();
				
	}
	
	public void updateUser(user user) {
		// TODO �Զ����ɵķ������
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = s.beginTransaction();
		s.update(user);
		tx.commit();
		
	}

}
